﻿using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using NetFtp.Core.Utils;
using NetFtp.Core.Ftp;

public partial class UploadFile : System.Web.UI.Page
{
    private void envoyer(FileUpload upfile, CheckBox chk, string folder)
    {
        Queue liste = new Queue();
        Queue listeftp = new Queue();
        
        //on enregistre le fichier sur le serveur local
        upfile.SaveAs(Path.Combine(folder, upfile.FileName));
        //si il faut le decompresser
        if (chk.Checked)
        {
            //on decompresse le fichier et on recupere la liste des fichiers et dossiers
            liste = ZipUtils.Unzip(Path.Combine(folder, upfile.FileName), folder);
            listeftp = (Queue)liste.Clone();
        }

        //envoie
        FtpSession ftp = new FtpSession(Session["HOST_FTP"].ToString(), int.Parse(Session["PORT_FTP"].ToString()), Session["LOGIN_FTP"].ToString(), Session["MDP_FTP"].ToString(), bool.Parse(Session["PASSIVE_FTP"].ToString()));
        while (listeftp.Count != 0)
        {
            FtpFile filetmp = new FtpFile(Path.Combine(folder, listeftp.Dequeue().ToString()));
            ftp.Upload(filetmp.FullName, Path.Combine(ftpfolder.Value, filetmp.Name));
        }
        FileInfo file = new FileInfo(Path.Combine(folder, upfile.FileName));
        if (chk.Checked == false)
            ftp.Upload(file.FullName, Path.Combine(ftpfolder.Value, file.Name));

        //on supprimer les fichier du serveur local
        while (liste.Count != 0)
        {
            FileInfo filetmp = new FileInfo(Path.Combine(folder, liste.Dequeue().ToString()));
            filetmp.Delete();
        }
        //on supprime le fichier de base
        file.Delete();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USER_LOGIN"] == null)
            Response.Redirect("Default.aspx");

        if (Page.IsPostBack == false)
            ftpfolder.Value = Request.QueryString["folder"];
    }

    protected void cmdOk_Click(object sender, EventArgs e)
    {
        string msg = "";

        try
        {
            if (file1.HasFile)
            {
                envoyer(file1, unzip1, Session["FOLDER_SESSION"].ToString());
                msg = msg + file1.FileName + GetGlobalResourceObject("General", "FichierEnvoyer").ToString();
            }
            if (file2.HasFile)
            {
                envoyer(file2, unzip2, Session["FOLDER_SESSION"].ToString());
                msg = msg + file2.FileName + GetGlobalResourceObject("General", "FichierEnvoyer").ToString();
            }
            if (file3.HasFile)
            {
                envoyer(file3, unzip3, Session["FOLDER_SESSION"].ToString());
                msg = msg + file3.FileName + GetGlobalResourceObject("General", "FichierEnvoyer").ToString();
            }
            if (file4.HasFile)
            {
                envoyer(file4, unzip4, Session["FOLDER_SESSION"].ToString());
                msg = msg + file4.FileName + GetGlobalResourceObject("General", "FichierEnvoyer").ToString();
            }
            if (file5.HasFile)
            {
                envoyer(file5, unzip5, Session["FOLDER_SESSION"].ToString());
                msg = msg + file5.FileName + GetGlobalResourceObject("General", "FichierEnvoyer").ToString();
            }
        }
        catch (Exception ex)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "js", "<script>alert('Error : " + ex.Message.Replace(@"'", @"") + "');</script>");
            return;
        }

        if (msg != "")
            ClientScript.RegisterStartupScript(this.GetType(), "js1", "<script>alert('" + msg + "');window.close();</script>");
        else
            ClientScript.RegisterStartupScript(this.GetType(), "js1", "<script>window.close();</script>");
    }

    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "js1", "<script>window.close();</script>");
    }
}
